package com.warehouse_management.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.warehouse_management.config.PageModel;
import com.warehouse_management.purchase.model.Firm;
import com.warehouse_management.purchase.model.FirmCondition;
import com.warehouse_management.purchase.service.ManufacturerService;
import com.warehouse_management.system.model.City;
import com.warehouse_management.system.model.Province;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @PACKAGE_NAME: com.warehouse_management.purchase.controller
 * @NAME: ManufacturerController
 * @USER: ZhouYongCheng
 * @DATE: 2022/10/3
 * @TIME: 2:46
 * @Description: 厂商管理控制层
 */
@Controller
@RequestMapping("/manufacturer")
public class ManufacturerController {

    @Autowired
    private ManufacturerService manufacturerService;


    @RequestMapping("/queryAllFirm")
    @ResponseBody
    public String queryAllFirm(@RequestParam("pagenum") int pagenum, FirmCondition firm, PageModel<FirmCondition> pageModel){
        pageModel.setPageNum(pagenum);
        PageModel<FirmCondition> pageModel1 = manufacturerService.PageFirmPosition(firm, pageModel);
        if (pageModel1.getData() != null){
            if (!pageModel1.getData().isEmpty()){
                System.out.println(pageModel1);
                return JSON.toJSONString(pageModel1);

            }
        }
        return null;
    }

    /**
     * 连小龙
     * 查询单个厂商数据(模糊查询)
     * @param fconpany
     * @param fname
     * @param provin
     * @param cit
     * @param statu
     * @return
     */
    @RequestMapping("/queryOneFirm")
    @ResponseBody
    public String queryOneFirm(@RequestParam("pagenum") int pagenum,@RequestParam("fconpany") String fconpany,@RequestParam("fname") String fname,@RequestParam("provin") String provin,@RequestParam("cit") String cit,@RequestParam("statu") String statu, PageModel<FirmCondition> pageModel){
        FirmCondition f = new FirmCondition();
        Province province = new Province();
        City city = new City();
        if (!provin.equals("请选择省份")){
            f.setPName(provin);
            province.setPName(provin);
        }
        if (!cit.equals("请选择城市")){
            f.setC_Name(cit);
            city.setC_Name(cit);
        }
        f.setFirmFounder(fname==null?null:fname);
        f.setStatus(statu);
        f.setFirmName(fconpany==null?null:fconpany);
        f.setProvince(province);
        f.setCity(city);
        pageModel.setPageNum(pagenum);
        PageModel<FirmCondition> firmListByCondition = manufacturerService.queryFirmListByCondition(f, pageModel);
        if (firmListByCondition != null){
            System.out.println(firmListByCondition);
                return JSON.toJSONString(firmListByCondition);
        }
        return null;
    }

    @RequestMapping("/queryProvince")
    @ResponseBody
    public String queryProvince(){
        List<Province> provinceList = manufacturerService.queryProvince();
        if (provinceList != null){
            System.out.println(provinceList);
            return JSON.toJSONString(provinceList);
        }
        return null;
    }

    @RequestMapping("/queryCity")
    @ResponseBody
    public String queryCity(@RequestParam("id") String id){
        List<City> cityList = manufacturerService.queryCity(id);
        if (cityList != null){
            System.out.println(cityList);
            return JSON.toJSONString(cityList);
        }
        return null;
    }

    /**
     * 连小龙
     * 查看单个数据详情
     * @param id
     * @return
     */
    @RequestMapping("/queryFirmDetails")
    @ResponseBody
    public String queryFirmDetails(@RequestParam("id") String id,PageModel<FirmCondition> pageModel){
        FirmCondition fc = new FirmCondition();
        fc.setFirmId(id);
        PageModel<FirmCondition> firmConditionPageModel = manufacturerService.queryFirmListByCondition(fc, pageModel);
        if (firmConditionPageModel != null){
            System.out.println(firmConditionPageModel);
            return JSON.toJSONString(firmConditionPageModel);
        }
        return null;
    }

    @RequestMapping("/addFirm")
    @ResponseBody
    public String addFirm(@RequestParam("Fname") String Fname,@RequestParam("founder") String founder,@RequestParam("tel") String tel,@RequestParam("address") String address,
                          @RequestParam("city") String city,@RequestParam("content") String content,@RequestParam("status") String status,
                          @RequestParam("creatName") String creatName){

        FirmCondition firm = new FirmCondition();
        firm.setFirmName(Fname);
        firm.setFirmAddress(address);
        firm.setFirmContent(content);
        firm.setFirmFounder(founder);
        firm.setFirmTel(tel);
        firm.setStatus(status);
        firm.setC_Name(city);
        firm.setCreatName(creatName);
        int i = manufacturerService.addFirm(firm);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    @RequestMapping("/updateFirm")
    @ResponseBody
    public String updateFirm(@RequestParam("id") String id,@RequestParam("Fname") String Fname,@RequestParam("founder") String founder,@RequestParam("tel") String tel,@RequestParam("address") String address,
                          @RequestParam("city") String city,@RequestParam("content") String content,@RequestParam("status") String status){

        FirmCondition firm = new FirmCondition();
        firm.setFirmName(Fname);
        firm.setFirmAddress(address);
        firm.setFirmContent(content);
        firm.setFirmFounder(founder);
        firm.setFirmTel(tel);
        firm.setStatus(status);
        firm.setC_Name(city);
        firm.setFirmId(id);
        int i = manufacturerService.updateFirm(firm);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }
}
